# 遍历数组
# https://www.bilibili.com/video/BV19T4y127Z2?p=5&spm_id_from=pageDriver&vd_source=8bd7b24b38e3e12c558d839b352b32f4
import numpy as np

print("遍历数组")

# 遍历一维数组
a = np.arange(11) ** 2
print(a)
for i in a:
    # 平方根
    print(i ** (1 / 2))

# 遍历二维数组 例1
students = np.array([['Dave', 'Nick', 'Martin', 'Sarah'],
              ['98', '78', '65', '99'],
              ['96', '77', '89', '100']])
print(students)
# 1.
print("#################################")
for e in students:
    print(e)
# 2. flatten
print("#################################")
for e2 in students.flatten():
    print(e2)
# 3. flatten(order="F")
print("#################################")
for e3 in students.flatten(order="F"):
    print(e3)


# 遍历二维数组 例2
# 说明：flatten会返回一个数组，nditer只是进行遍历
print("#################################")
x = np.arange(12).reshape(3, 4)
print(x)
# 与flatten默认情况差不多
print("#################################")
for i in np.nditer(x):
    print(i)
# 类似flatten(order="F")
print("#################################")
for i in np.nditer(x, order="F"):
    print(i)
print("#################################")
for i in np.nditer(x, order="F", flags=["external_loop"]):
    print(i)
print("#################################")
for i in np.nditer(x, op_flags=["readwrite"]):
    # i[...] 表示当前元素
    i[...] = i * i
print(x)